﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;

using System.Net;
using System.Windows;
using OS.GenericDashboardViewer.DataModel;
using System.Windows.Forms;
using System.Windows.Forms.Integration;



namespace OS.GenericDashboardViewer.WPF
{
   
    public static class WebBrowserUtility
    {
        [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

        [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern IntPtr GetCurrentProcess();
        public static readonly DependencyProperty BindableSourceProperty =
            DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, BindableSourcePropertyChanged));

        public static string GetBindableSource(DependencyObject obj)
        {
            return (string)obj.GetValue(BindableSourceProperty);
        }

        public static void SetBindableSource(DependencyObject obj, string value)
        {
            obj.SetValue(BindableSourceProperty, value);
        }

        public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            WindowsFormsHost windowsFormsHost = o as WindowsFormsHost;
            if (windowsFormsHost != null && e.NewValue != null)
            {
                //TODO:Ugly 
         //       var viewHolder = e.NewValue as WebViewPartHolder;
                ///////////////////////////////////////
                var webVP =windowsFormsHost.DataContext as WebViewPart;
                var viewHolder = new { Zoom = webVP.Zoom, Url = webVP.Url };
                //////////////////////
                if (windowsFormsHost.Child != null)
                {
                    WebBrowser toDispWb = windowsFormsHost.Child as ZoomableWebBrowser;
                    toDispWb.Navigated -= Browser_Navigated;
                    toDispWb.Dispose();
                    windowsFormsHost.Child = null;
                    //solving memory leak hack
                    IntPtr pHandle = GetCurrentProcess();
                    SetProcessWorkingSetSize(pHandle, -1, -1);
                }

                WebBrowser webBrowser = new ZoomableWebBrowser();
                windowsFormsHost.Child = webBrowser;
                
                webBrowser.Url = new Uri(viewHolder.Url);
                webBrowser.Tag = viewHolder.Zoom;

                webBrowser.Navigated += (Browser_Navigated);
            }
        }

        static void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            var wb = (ZoomableWebBrowser)sender;
            wb.Zoom((int)wb.Tag);
        }



    }

}
